<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: ''
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        title: {
          text: '用电曲线'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['今日', '昨日']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '今日',
            type: 'line',
            data: [12, 13, 15, 12, 16, 21, 12, 23, 32, 26, 27, 27, 28, 26, 24, 22, 22, 19, 30, 28, 29, 29]
          },
          {
            name: '昨日',
            type: 'line',
            data: [11, 18, 22, 12, 22, 32, 11, 32, 12, 23, 23, 25, 22, 23, 23, 23, 23, 26, 26, 25, 28, 30]
          }
        ]
      })
    }
  }
}
</script>
